/*
 * Power supply driver for the fake emulator
 *
 * Copyright (C) 2008 Google, Inc.
 * Copyright (C) 2012 Intel, Inc.
 * Copyright (C) 2013 Intel, Inc.
 * Author: Mike Lockwood <lockwood@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/acpi.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/of.h>
/**
 * dts config
 * 	fake_battery {
		compatible = "google,fake-battery";
		status = "okay";
	};
 */
#define BUFF_SIZE 128
#define MONITOR_SEC_DEFAULT (1*1000)

struct fake_battery_data {
	int reg_base[BUFF_SIZE];
	spinlock_t lock;
	struct power_supply *battery;
	struct power_supply *ac;
	struct workqueue_struct *battery_workqueue;
	struct delayed_work battery_delay_work;
	// data from dts
	int monitor_sec;
};




#define fake_BATTERY_READ(data, addr) \
	(data->reg_base[addr])

#define fake_BATTERY_WRITE(data, addr, x) \
	(data->reg_base[addr]=x)


static struct fake_battery_data *battery_data;

enum {
	/* status register */
	BATTERY_INT_STATUS	    = 0x00,
	/* set this to enable IRQ */
	BATTERY_INT_ENABLE	    = 0x04,

	BATTERY_AC_ONLINE       = 0x08,
	BATTERY_STATUS          = 0x0C,
	BATTERY_HEALTH          = 0x10,
	BATTERY_PRESENT         = 0x14,
	BATTERY_CAPACITY        = 0x18,

	BATTERY_STATUS_CHANGED	= 1U << 0,
	AC_STATUS_CHANGED	= 1U << 1,
	BATTERY_INT_MASK        = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
};


static int fake_ac_get_property(struct power_supply *psy,
			enum power_supply_property psp,
			union power_supply_propval *val)
{
	struct fake_battery_data *data = power_supply_get_drvdata(psy);
	int ret = 0;

	switch (psp) {
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = fake_BATTERY_READ(data, BATTERY_AC_ONLINE);
		break;
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}

static int fake_battery_get_property(struct power_supply *psy,
				 enum power_supply_property psp,
				 union power_supply_propval *val)
{
	struct fake_battery_data *data = power_supply_get_drvdata(psy);
	int ret = 0;

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = fake_BATTERY_READ(data, BATTERY_STATUS);
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		val->intval = fake_BATTERY_READ(data, BATTERY_HEALTH);
		break;
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = fake_BATTERY_READ(data, BATTERY_PRESENT);
		break;
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
		val->intval = fake_BATTERY_READ(data, BATTERY_CAPACITY);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

static enum power_supply_property fake_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_HEALTH,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CAPACITY,
};

static enum power_supply_property fake_ac_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
};


static const struct power_supply_desc battery_desc = {
	.properties	= fake_battery_props,
	.num_properties	= ARRAY_SIZE(fake_battery_props),
	.get_property	= fake_battery_get_property,
	.name		= "battery",
	.type		= POWER_SUPPLY_TYPE_BATTERY,
};

static const struct power_supply_desc ac_desc = {
	.properties	= fake_ac_props,
	.num_properties	= ARRAY_SIZE(fake_ac_props),
	.get_property	= fake_ac_get_property,
	.name		= "ac",
	.type		= POWER_SUPPLY_TYPE_MAINS,
};


static void parse_dts(struct platform_device *pdev)
{
	struct fake_battery_data *data = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;
	struct device_node  *np = dev->of_node;
	(void)np;

	if(of_property_read_u32(np, "monitor_sec", &data->monitor_sec)){
		data->monitor_sec = MONITOR_SEC_DEFAULT;
	}
	fake_BATTERY_WRITE(data, BATTERY_CAPACITY, 80);
	fake_BATTERY_WRITE(data, BATTERY_STATUS, 1);
	fake_BATTERY_WRITE(data, BATTERY_HEALTH, POWER_SUPPLY_HEALTH_GOOD);
	fake_BATTERY_WRITE(data, BATTERY_AC_ONLINE, 1);
}

static void dump(void)
{
	pr_info("BATTERY_CAPACITY:%d\n", fake_BATTERY_READ(battery_data, BATTERY_CAPACITY));
	pr_info("BATTERY_HEALTH:%d\n", fake_BATTERY_READ(battery_data, BATTERY_HEALTH));
	pr_info("BATTERY_AC_ONLINE:%d\n", fake_BATTERY_READ(battery_data, BATTERY_AC_ONLINE));
	pr_info("BATTERY_STATUS:%d\n", fake_BATTERY_READ(battery_data, BATTERY_STATUS));
}


static void fake_bat_work(struct work_struct *work)
{
	struct delayed_work *delay_work ;
	struct fake_battery_data *data;
	int cap = 0;
	delay_work =container_of(work, struct delayed_work, work);
	data = container_of(delay_work, struct fake_battery_data, battery_delay_work);
	cap= fake_BATTERY_READ(battery_data, BATTERY_CAPACITY);
	cap += 1;
	if(cap >100){
		cap = 0;
	}
	fake_BATTERY_WRITE(data, BATTERY_CAPACITY, cap);

	power_supply_changed(data->battery); // notify app battery change, otherwise system battery not refresh

	queue_delayed_work(data->battery_workqueue,
			   &data->battery_delay_work,
			   msecs_to_jiffies(data->monitor_sec));
}

static int fake_battery_probe(struct platform_device *pdev)
{
//	int ret;
	struct fake_battery_data *data;
	struct power_supply_config psy_cfg = {};

	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (data == NULL)
		return -ENOMEM;

	spin_lock_init(&data->lock);


	psy_cfg.drv_data = data;

	data->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
	if (IS_ERR(data->ac))
		return PTR_ERR(data->ac);

	data->battery = power_supply_register(&pdev->dev, &battery_desc,
						&psy_cfg);
	if (IS_ERR(data->battery)) {
		power_supply_unregister(data->ac);
		return PTR_ERR(data->battery);
	}

	platform_set_drvdata(pdev, data);
	parse_dts(pdev);
	data->battery_workqueue = create_singlethread_workqueue("fake_battery");
	INIT_DELAYED_WORK(&data->battery_delay_work, fake_bat_work);
	queue_delayed_work(data->battery_workqueue,
			   &data->battery_delay_work, msecs_to_jiffies(10));
	battery_data = data;
	dump();
	return 0;
}

static int fake_battery_remove(struct platform_device *pdev)
{
	struct fake_battery_data *data = platform_get_drvdata(pdev);

	power_supply_unregister(data->battery);
	power_supply_unregister(data->ac);
	battery_data = NULL;
	return 0;
}

static const struct of_device_id fake_battery_of_match[] = {
	{ .compatible = "google,fake-battery", },
	{},
};
MODULE_DEVICE_TABLE(of, fake_battery_of_match);

static const struct acpi_device_id fake_battery_acpi_match[] = {
	{ "GFSH0001", 0 },
	{ },
};
MODULE_DEVICE_TABLE(acpi, fake_battery_acpi_match);

static struct platform_driver fake_battery_device = {
	.probe		= fake_battery_probe,
	.remove		= fake_battery_remove,
	.driver = {
		.name = "fake-battery",
		.of_match_table = fake_battery_of_match,
		.acpi_match_table = ACPI_PTR(fake_battery_acpi_match),
	}
};
module_platform_driver(fake_battery_device);

MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Battery driver for the FAKE emulator");
